package com.dlc.yunguang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;

import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.TimeFormats;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.activitynew.SearchBTActivity;
import com.dlc.yunguang.adapter.OrderDetailAdapter1;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.OrderDetailBean;
import com.dlc.yunguang.utils.BluetoothUtil;
import com.dlc.yunguang.R;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class OrderDetailActivity1 extends BaseActivity {


    OrderDetailBean orderDetailBean;
    OrderDetailAdapter1 mOrderDetailAdapter;
    @BindView(R.id.wash_goods_hint)
    TextView washGoodsHint;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.company)
    TextView company;
    @BindView(R.id.send_wash_code)
    TextView sendWashCode;
    @BindView(R.id.nums)
    TextView nums;
    @BindView(R.id.send_wash_time)
    TextView sendWashTime;
    @BindView(R.id.reveice_time)
    TextView reveiceTime;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.print)
    Button print;
    private boolean isBold, isUnderLine;
    private int record;
    int type;


    @Override
    protected int getLayoutID() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftExit(this);

        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");
        orderDetailBean = (OrderDetailBean) bundle.getSerializable("detailData");

        if (orderDetailBean.data.spec_group.size() > 0) {
            InitRecyclerView(orderDetailBean);
            company.setText(orderDetailBean.data.company);
        }
        if (orderDetailBean.data.orderno.contains("W")) {
            washGoodsHint.setText(getString(R.string.send_wash_code));
        } else if (orderDetailBean.data.orderno.contains("D")) {
            washGoodsHint.setText(getString(R.string.send_goods_code));
        } else if (orderDetailBean.data.orderno.contains("T")) {
            washGoodsHint.setText("退貨單號");
        } else {
            washGoodsHint.setText("返洗單號");
        }
        sendWashTime.setText(
            TimeFormats.formatDateTimeSecend((long) Integer.parseInt(orderDetailBean.data.ctime) * 1000));
        if (TextUtils.isEmpty(orderDetailBean.data.receivetime) || orderDetailBean.data.receivetime.equals("0")) {
            reveiceTime.setText(getString(R.string.not_yet_received));
        } else {
            reveiceTime.setText(TimeFormats.formatDateTimeSecend((long) Integer.parseInt(orderDetailBean.data.receivetime) * 1000));
        }
        sendWashCode.setText(orderDetailBean.data.orderno);
        nums.setText(orderDetailBean.data.nums);


        isBold = false;
        isUnderLine = false;
        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = "";
              //  printB();
                String orderno = orderDetailBean.data.orderno;
                string = "";
                string =string+"  海恩康（天津）医疗科技有限公司"  + "\r\n";
                if (orderno.contains("W")) {
                    string = string+"         " + getString(R.string.washing_oeder) + "\r\n" ;
                } else if (orderno.contains("D")) {
                    string =string+ "          " + getString(R.string.receiving_note) + "\r\n" ;
                }else if (orderno.contains("I")) {
                    string =string+ "          " + "科室分发单" + "\r\n" ;
                }


                string =string+"单    号："  +  orderno + "\r\n";
                string = string  + "名    称：" + orderDetailBean.data.hotel + "\r\n";
                if (!TextUtils.isEmpty(orderDetailBean.data.office)) {
                    string = string + "医院科室：" + orderDetailBean.data.office + "\r\n";
                }
                string =string+"总 数 量：" +  orderDetailBean.data.nums + "\r\n";

                SimpleDateFormat formatter   =   new   SimpleDateFormat   ("yyyy-MM-dd HH:mm");
                Date curDate =  new Date(System.currentTimeMillis());
                String   date   =   formatter.format(curDate);
                string = string +  "日    期：" + date+ "\r\n";

                string = string + "-------------------------";

                for (int i = 0; i < orderDetailBean.data.spec_group.size(); i++) {
                    OrderDetailBean.DataBean.SpecGroupBean bean = orderDetailBean.data.spec_group.get(i);
                    string = string + "\r\n" +( bean.spec  + "   "+bean.count);
                    string = string + "\r\n" +("人员：" +bean.user);
                    string = string + "\r\n";
                }
                string = string + "-------------------------";
                string = string + "\r\n";

                string = string + "备注：" + "\r\n" ;
                string = string + "□ 污染" + "\r\n" ;
                string = string + "□ 破损";
                string = string + "\r\n"+ "\r\n";
                string = string + getString(R.string.signature) + "：" + "\r\n" + "\r\n"+ "\r\n";
                string = string + "-------------------------";
                string = string + "\r\n"+ "\r\n";

                LogPlus.e("string="+string);
                Intent intent = new Intent(getActivity(), SearchBTActivity.class);
                intent.putExtra(SearchBTActivity.key_string, string);
                startActivity(intent);
                //rv = BytesUtil.getBytesFromHexString();
                //AidlUtil.getInstance().sendRawData(BytesUtil.getBytesFromHexString(text));
                //sendData(rv);
               // sendData(string,22);
            }
        });
    }



    private void InitRecyclerView(OrderDetailBean orderDetailBean) {
        mOrderDetailAdapter = new OrderDetailAdapter1();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mOrderDetailAdapter);
        if (orderDetailBean.data.spec_group != null) {
            mOrderDetailAdapter.addData(orderDetailBean.data.spec_group);
        }
    }

    public static String convertTime(Long time) {
        time = time * 1000;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(new Date(time));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothUtil.disconnectBlueTooth(this);
    }



}
